library("RSNNS")                                                                        #load the package RSNNS
source("f_measures.R")                                                                   #load the function to compute the F1-score into the workspace

number.of.tests <- 100                                                                  #define the number of tests you want to perform
results <- data.frame(training=rep(NA, 100), test= rep(NA, 100))                        #create an empty vector to store the results

for (test.index in 1:100) {                                                             #loop
  print(paste("Test number:", test.index))
  
  iris.shuffled <- iris[sample(1:nrow(iris),nrow(iris)),]                             #shuffle the lines of the dataset
  
  iris.inputs <- iris.shuffled[,1:4]                                                  #copy the input columns in a separate variable
  iris.targets <- decodeClassLabels(iris.shuffled[,5], valTrue=0.95, valFalse=0.05)   #decode the classes into a binary representation
  
  iris.split <- splitForTrainingAndTest(iris.inputs, iris.targets, ratio=0.15)        #split the dataset in 2 parts: training and test
  iris.normalized <- normTrainingAndTestSet(iris.split)                               #normalize both parts (training and test)
  
  
  model <- mlp(   x=iris.split$inputsTrain,                                           #input data for training
                  y=iris.split$targetsTrain,                                          #output data (targets) for training
                  size=5,                                                             #number of neurons in the hidden layer
                  learnFunc="Std_Backpropagation",                                    #type of learning
                  learnFuncParams=c(0.1),                                             #paramenters of the learning function (eta)
                  maxit=100,                                                          #maximum number of iterations
                  inputsTest=iris.split$inputsTest,                                   #input data for testing
                  targetsTest=iris.split$targetsTest)                                 #output data (targets) for testing
  
  prediction.training <- predict(model, iris.split$inputsTrain)                       #compute the outputs of the MLP for the training dataset
  prediction.test <- predict(model, iris.split$inputsTest)                            #compute the outputs of the MLP for the test dataset
  
  target.training.class <- apply(iris.split$targetsTrain, 1, which.max)               #find the target class for each training example
  target.test.class <- apply(iris.split$targetsTest, 1, which.max)                    #find the target class for each test example
  
  prediction.training.class <- apply(prediction.training, 1, which.max)               #find the output with the highest activation for each training example
  prediction.test.class <- apply(prediction.test, 1, which.max)                       #find the output with the highest activation for each test example
  
  results$training[test.index] <- f.measure(target.training.class, prediction.training.class)     #compute the F1-score for the training dataset
  results$test[test.index] <- f.measure(target.test.class, prediction.test.class)                 #compute the F1-score for the test dataset
  
}

boxplot(results, notch=TRUE)                                                            #plot the results
grid()